package jd.se.lend;
import jd.se.common.CurrentBook;
import jd.se.common.CurrentUser;
import jd.se.common.Result;
import jd.se.common.Utils;


import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.*;
import java.util.UUID;

@MultipartConfig
@WebServlet({

        "/lend",
        "/cover",
        "/current-book",
})
public class LendAPI extends HttpServlet {
    @Override
    public void init() throws SecurityException{
        File fen=new File(CurrentBook.FEN_M);
        if(!fen.exists()){
            fen.mkdirs();
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        LendService service = new LendService();
        request.setCharacterEncoding("UTF-8");
        String path = request.getServletPath();

        if("/lend".equals(path)){

            LendDto dto = Utils.getBeanFromRequest(LendDto.class,request);
            CurrentUser currentUser = (CurrentUser)request.getSession().getAttribute(CurrentUser.SESSION_ATTR_NAME);
            LendService.addLend(dto,currentUser);
            HttpSession session = request.getSession();
            session.setAttribute(CurrentBook.SESSION_ATTR_NAME,new CurrentBook(dto.getB_id()));
            Utils.outJson(response, Result.success());

        }
        if("/cover".equals(path)){
            Part part = request.getPart("cover");

            InputStream in = part.getInputStream();

            String name = UUID.randomUUID().toString();//获得通过uuid算法得到的不重复的字符串
            String submitedFilename = part.getSubmittedFileName();//原始文件名
            String suffix = submitedFilename.substring(submitedFilename.lastIndexOf("."));
            String filename = name+suffix;
            OutputStream out = new FileOutputStream(CurrentBook.FEN_M+"/"+filename);
            byte[] b = new byte[10240];//容量为10240字节的缓存区
            int len = -1;//从输入流读取并放入缓存区的数据量

            while((len=in.read(b))!=-1){
                out.write(b,0,len);
            }
            out.flush();
            out.close();
            in.close();

            CurrentBook currentBook = (CurrentBook)request.getSession().getAttribute(CurrentBook.SESSION_ATTR_NAME);
            service.setcover(currentBook,filename);

            Utils.outResult(response, Result.success());
        }






    }

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



        String path = request.getServletPath();//获取请求路径

        LendService service = new LendService();

        if("/current-book".equals(path)){

            CurrentBook currentBook = (CurrentBook)request.getSession().getAttribute(CurrentBook.SESSION_ATTR_NAME);
            System.out.println("currentBook->"+currentBook);
            service.loadCurrentBook(currentBook);//加载完整的用户信息

            Utils.outResult(response, Result.success(currentBook));

        }

        if("/cover".equals(path)){
            //从请求参数中通过参数名称filename获取文件名称
            String filename = request.getParameter("filename");
            //读取文件的输入流
            InputStream in = new FileInputStream(CurrentBook.FEN_M+"/"+filename);
            //将文件信息输出到浏览器的输出流
            OutputStream out = response.getOutputStream();

            byte[] b = new byte[10240];//容量为10240字节的缓存区
            int len = -1;//从输入流读取并放入缓存区的数据量

            while((len=in.read(b))!=-1){
                out.write(b,0,len);
            }
            out.flush();
            out.close();
            in.close();

        }
    }
}

